#!/usr/bin/env sh
set -euo pipefail

# https://frippery.org/busybox/index.html

# Usage:
#   ./fetch_msys2.sh 2025-08-30
#
# Fetch and extract https://github.com/msys2/msys2-installer/releases/download/<YYYY-MM-DD>/msys2-base-x86_64-<YYYYMMDD>.tar.xz

MSYS2_VERSION="${1:-2025-08-30}"

if [ -e "msys64" ]; then
    echo "outdir ./msys64/ exists."
    exit 1
fi

asset_url=https://github.com/msys2/msys2-installer/releases/download/${MSYS2_VERSION}/msys2-base-x86_64-${MSYS2_VERSION//-/}.tar.xz
echo "Download url: $asset_url"

curl -L -o msys2-base-x86_64.tar.xz "$asset_url"
tar -xf msys2-base-x86_64.tar.xz

sed -i "s/db_home: cygwin desc/db_home: \/home\/user1/" msys64/etc/nsswitch.conf

unset HOME
MSYSTEM="CLANG64" msys64/usr/bin/bash -l -c "echo hello msys64"

cp .vimrc msys64/home/user1/
cp .bashrc msys64/home/user1/
cp *.sh msys64/home/user1/

MSYSTEM="CLANG64" msys64/usr/bin/bash -l -i

